/*
 * Copyright 2007,2008 Anton Dignoes, Daniel Rampanelli, Martin Palma
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.webduff.store.compound;

import java.io.InputStream;
import java.util.Date;
import java.util.Hashtable;
import java.util.Map;

import com.googlecode.webduff.authentication.provider.Credential;
import com.googlecode.webduff.io.URI;
import com.googlecode.webduff.store.WebdavStore;
import com.googlecode.webduff.store.WebdavStoreFactory;


public class CompoundWebDuffStore implements WebdavStore {
	
	@SuppressWarnings("unused")
	private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(CompoundWebDuffStore.class);
	
	private WebdavStore RootStore;
	
	private Map<String, WebdavStore> theDefinedStores;
	
	public CompoundWebDuffStore(Credential credential, Map<String, WebdavStoreFactory> definedFactories) {
		theDefinedStores = new Hashtable<String, WebdavStore>();
		for(String storeName : definedFactories.keySet()) {
			theDefinedStores.put(storeName, definedFactories.get(storeName).create(credential));
		}
		
		RootStore = new RootWebDuffStore(theDefinedStores);
	}

	public void begin() {

	}

	public void commit() {

	}

	public void createFolder(URI uri) {
		getStoreForRequest(uri).createFolder(getURIForRequest(uri));
	}

	public void createResource(URI uri) {
		getStoreForRequest(uri).createResource(getURIForRequest(uri));
	}

	public String[] getChildrenNames(URI uri) {
		return getStoreForRequest(uri).getChildrenNames(getURIForRequest(uri));
	}

	public Date getCreationDate(URI uri) {
		return getStoreForRequest(uri).getCreationDate(getURIForRequest(uri));
	}

	public Date getLastModified(URI uri) {
		return getStoreForRequest(uri).getLastModified(getURIForRequest(uri));
	}

	public InputStream getResourceContent(URI uri) {
		return getStoreForRequest(uri).getResourceContent(getURIForRequest(uri));
	}

	public long getResourceLength(URI uri) {
		return getStoreForRequest(uri).getResourceLength(getURIForRequest(uri));
	}

	public boolean isFolder(URI uri) {
		return getStoreForRequest(uri).isFolder(getURIForRequest(uri));
	}

	public boolean isResource(URI uri) {
		return getStoreForRequest(uri).isResource(getURIForRequest(uri));
	}

	public boolean objectExists(URI uri) {
		return getStoreForRequest(uri).objectExists(getURIForRequest(uri));
	}

	public void removeObject(URI uri) {
		getStoreForRequest(uri).removeObject(getURIForRequest(uri));
	}

	public void rollback() {

	}

	public void setResourceContent(URI uri, InputStream content, String contentType, String characterEncoding) {
		getStoreForRequest(uri).setResourceContent(getURIForRequest(uri), content, contentType, characterEncoding);
	}
	
	public boolean canCreateFolder(URI uri) {
		return getStoreForRequest(uri).canCreateFolder(getURIForRequest(uri));
	}

	public boolean canCreateResource(URI uri) {
		return getStoreForRequest(uri).canCreateResource(getURIForRequest(uri));
	}

	public String getMimeType(URI uri) {
		return getStoreForRequest(uri).getMimeType(getURIForRequest(uri));
	}
	
	private WebdavStore getStoreForRequest(URI uri) {
		String firstToken = uri.getNthComponent(0);
		if(firstToken != null && theDefinedStores.containsKey(firstToken)) {
			return theDefinedStores.get(firstToken);
		}
		return RootStore;
	}
	
	private URI getURIForRequest(URI uri) {
		String firstToken = uri.getNthComponent(0);
		if(firstToken != null && theDefinedStores.containsKey(firstToken)) {
			if(uri.size() > 1) {
				return uri.stripFirst();
			}
			return new URI("/");
		} else {
			return uri;
		}
	}

}